package com.gem.netty.message.swap;

import com.gem.entity.Game;
import com.gem.entity.Nobility;
import com.gem.enums.ClientMessageType;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.utils.R;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Author: Xhy
 * CreateTime: 2022-12-14 22:05
 */
public class GetNobility extends AbstractSwapBase{

    @Override
    protected R isVerify(ClientContent content) {
        Nobility nobility = content.getContent().getNobility();
        if (nobility == null){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("非法操作").type(ClientMessageType.GET_GEM.type);
        }
        return R.ok();
    }

    @Override
    protected R isStock(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());
        Integer no = content.getContent().getNobility().getNo();
        String roomId = game.getRoomId();
        if (game.getNobilities().size() == 0 || (!Nobility.exist(roomId,no))){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("贵族库已空").type(ClientMessageType.GET_NOBILITY.type);
        }
        return R.ok();
    }

    @Override
    protected R isOwnNobility(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());

        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        if (user.getNobility()!=null){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("你已经拥有贵族").type(ClientMessageType.GET_NOBILITY.type);
        }
        return R.ok();
    }

    @Override
    protected R isSwap(ClientContent content) {
        Game game = GameFactory.get(content.getRoomId());
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        // 获取兑换贵族卡的信息
        Nobility nobility = Nobility.get(content.getContent().getNobility().getNo());
        AtomicBoolean flag = new AtomicBoolean(false);
        nobility.getCards().forEach((k,v)->{
            if ((user.getCards().get(k).size() - v ) < 0){
                flag.set(true);

                return;
            }
        });
        if (flag.get()){
            return R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("换不了贵族,卡牌不够").type(ClientMessageType.GET_NOBILITY.type);
        }
        return R.ok();
    }

    @Override
    protected R isSwapCardOrGetGemRound(ClientContent content) {
        return R.ok();
    }
}
